import * as api from '@/api/hmmm/questions'
import { simple as subSimple } from '@/api/hmmm/subjects'
import { simple as userSimple } from '@/api/base/users'
import { list as catalogList } from '@/api/hmmm/directorys'
import { list as tagsList } from '@/api/hmmm/tags'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { parseTime } from '@/utils/index'
import previewDialog from '../../components/questions-preview'
import 'lodash'

export default {
    components: {
        previewDialog
    },
    data() {
        return {
            tableKey: 0,
            dataList: [],
            listLoading: false,
            total: 0,
            requestParameters: {
                page: 1,
                pagesize: 5,
            },
            alertText: '',
            citySelect: {
                province: [],
                cityData: []
            },
            subjectList: [],
            catalogData: [],
            tagList: [],
            subID: 0,
            userList: [],
            catalogList: [],
            formData: {},
            previewQes: false,
        }
    },
    computed: {
        questionType() { return questionType },
        difficulty() { return difficulty },
        direction() { return direction },
    },
    mounted() {
        this.initialDate()
    },
    methods: {
        // 初始数据
        initialDate() {
            // 读取数据
            this.getList()
            this.getSubjectList()
            this.getCityData()
            this.getUserList()
            this.getCatalogList()
        },
        // 每页显示信息条数
        handleSizeChange(val) {
            this.requestParameters.pagesize = val
            if (this.requestParameters.page === 1) {
                this.getList(this.requestParameters)
            }
        },
        // 进入某一页
        handleCurrentChange(val) {
            this.requestParameters.page = val
            this.getList()
        },
        // 当学科ID变化时
        handleSubjectChange(val) {
            delete this.requestParameters.catalogID
            delete this.requestParameters.tags
            this.getCatalogData(val)
            this.getTagsData(val)
        },
        // 重置
        resetForm() {
            this.$refs.requestParameters.resetFields()
            this.requestParameters = _.omitBy(this.requestParameters, (value, key) => _.isEmpty(value) && !_.isNumber(value))
        },

        /************搜索模块*****************/

        // 获取列表数据
        async getList() {
            this.listLoading = true
            const { data: res } = await api.list(this.requestParameters)
            // 获取数据给dataList
            // console.log(res);
            this.dataList = res.items
            // 获取总条数
            this.total = res.counts
            this.alertText = `共 ${this.total} 条记录`
            this.listLoading = false
        },
        // 获取学科列表
        async getSubjectList() {
            const res = await subSimple()
            this.subjectList = res.data
        },
        // 获取目录列表
        async getCatalogList() {
            const { data: res } = await catalogList()
            this.catalogList = res.items
        },
        // 选学科获取二级目录
        getCatalogData(id) {
            this.catalogData = this.catalogList.filter(item => item.subjectID === id)
        },
        // 获取标签列表
        async getTagsData(id) {
            const { data: res } = await tagsList({ subjectID: id })
            this.tagList = res.items
        },
        // 获取用户列表
        async getUserList() {
            const res = await userSimple()
            this.userList = res.data
        },
        // 获取省
        getCityData() {
            this.citySelect.province = provinces()
        },
        // 选省获取到市
        handleProvince(e) {
            delete this.requestParameters.city
            this.citySelect.cityData = citys(e)
        },
        // 搜索信息
        handleFilter() {
            this.requestParameters.page = 1
            this.getList(this.requestParameters)
        },

        /************数据格式化***************/

        // 格式化学科
        formatterSubject(row, column, cellValue, index) {
            const item = this.subjectList.find(item => item.value === cellValue)
            return item ? item.label : ''
        },
        // 格式化目录
        formatterCatalog(row, column, cellValue, index) {
            const item = this.catalogList.find(item => item.id == cellValue)
            return item ? item.directoryName : ''
        },
        // 格式化题型
        formatterType(row, column, cellValue, index) {
            const item = questionType.find(item => item.value == cellValue)
            return item ? item.label : ''
        },
        // 格式化难度
        formatterDiff(row, column, cellValue, index) {
            const item = difficulty.find(item => item.value == cellValue)
            return item ? item.label : ''
        },
        // 格式化时间
        formatterTime(row, column, cellValue, index) {
            // return moment(cellValue).format('YYYY-MM-DD hh-mm-ss')
            return parseTime(cellValue)
        },
        // 格式化题干
        formatterQuestion(row, column, cellValue, index) {
            return cellValue.replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, '').replace(/<[^>]+?>/g, '').replace(/\s+/g, ' ').replace(/ /g, ' ').replace(/>/g, ' ')
        },
        /*************列表操作*********************/
        //删除
        removeQuestion(question) {
            this.$confirm('此操作将永久删除该试题 ' + ', 是否继续?', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    await api.remove({ id: question })
                        .then(response => {
                            this.$message.success('试题删除成功' + '!')
                            this.getList(this.requestParameters)
                        })
                        .catch(response => {
                            this.$message.error('删除失败!')
                        })
                })
                .catch(() => {
                    this.$message.info('已取消操作!')
                })
        },
        //加入精选题库
        choiceQuestion(question) {
            this.$confirm('此操作将该题目加入精选' + ', 是否继续?', '提示', {
                type: 'info'
            })
                .then(async () => {
                    await api.choiceAdd({ id: question, choiceState: 1 })
                        .then(response => {
                            this.$message.success('加入精选成功' + '!')
                            this.getList(this.requestParameters)
                        })
                        .catch(response => {
                            this.$message.error('加入精选失败!')
                        })
                })
                .catch(() => {
                    this.$message.info('已取消操作!')
                })
        },
        /*****************窗口操作*************************/
        // 表单详情数据加载
        async hanldePreviewQuestion(objeditId) {
            this.formData.id = objeditId
            const { data: res } = await api.detail({ id: objeditId })
            // 获取详情 给formData
            this.formData = res
            this.previewQes = true
            this.$nextTick(() => {
                this.$refs.previewQuestion.dialogFormV()
            })
        },
    }
}